package com.ceba.test.modules.usermanage.service;


import com.ceba.base.web.service.IBaseService;
import org.json.JSONObject;

import com.ceba.test.modules.usermanage.dao.LoginDao;
import com.ceba.test.modules.usermanage.entity.UUser;

import abc.data.DataRow;
import abc.data.DataTable;
import abc.util.StringUtils;

public class LoginService extends IBaseService {
	
	private LoginDao loginDao;
			
	private void copyDrToUUser(DataRow dr,UUser user){
		JSONObject object = dr.toJSONObject();
		user.setId(dr.getString("ID"));
		user.setName(dr.getString("NAME"));
		user.setUserRole(object.has("ROLE")?object.getString("ROLE"):"");
		user.setPassword(object.getString("PASSWORD"));
		user.setLoginIndex(object.has("LOGIN_INDEX")?object.getString("LOGIN_INDEX"):"");
		user.setLoginIndexSignVer(object.has("LOGIN_INDEX_SIGNVER")?object.getString("LOGIN_INDEX_SIGNVER"):"");
		user.setLoingIndexSignOnly(object.has("LOGIN_INDEX_SIGNONLY")?object.getString("LOGIN_INDEX_SIGNONLY"):"");
		user.setDepartmentId(object.has("department_ID")?object.getString("department_ID"):"");
		user.setStep(object.has("Step")?object.getString("Step"):"");
	}

	private void copyAdDrToUUser(DataRow dr,UUser user){
		JSONObject object = dr.toJSONObject();
		user.setId(dr.getString("ID"));
		user.setName(dr.getString("NAME"));
		user.setUserRole(object.has("ROLE")?object.getString("ROLE"):"");
		user.setLoginIndex(object.has("LOGIN_INDEX")?object.getString("LOGIN_INDEX"):"");
		user.setLoginIndexSignVer(object.has("LOGIN_INDEX_SIGNVER")?object.getString("LOGIN_INDEX_SIGNVER"):"");
		user.setLoingIndexSignOnly(object.has("LOGIN_INDEX_SIGNONLY")?object.getString("LOGIN_INDEX_SIGNONLY"):"");
		user.setDepartmentId(object.has("department_ID")?object.getString("department_ID"):"");
		user.setStep(object.has("Step")?object.getString("Step"):"");
	}
	
    public UUser login(String loginID) throws Exception{
       DataTable dt = loginDao.login(loginID);
       if(dt !=null && dt.getRowCount() >0){
    	   UUser user = new UUser();
    	   copyDrToUUser(dt.getRow(0),user);
    	   return user;
       }
	   return null;
	}
    public UUser adLogin(String loginID) throws Exception{
       DataTable dt = loginDao.login(loginID);
       if(dt !=null && dt.getRowCount() >0){
    	   UUser user = new UUser();
		   copyAdDrToUUser(dt.getRow(0),user);
    	   return user;
       }
	   return null;
	}

    /**
	 * 保存密码
	 * @param pw
	 * @param userId
	 * @return
	 */
	public Boolean savePassword(String pw,String userId){
		return loginDao.savePassword(pw, userId);
	}
    /**
	 * 获取department的step值
	 * @param userId
	 * @param field字段
	 */
	public String getUserDepartmentFieldValueByUserId(String userId,String field){
		return loginDao.getUserDepartmentFieldValueByUserId(userId,field);
	}
	
	/**
	 * 根据查询条件获取部门
	 * @param condition
	 * @return
	 */
	public DataTable getDepartmentsByCondition(String condition){
		return loginDao.getDepartmentsByCondition(condition);
	}
    /**
	 * 获取当前用户的管辖权限
	 * @param userId 用户ID
	 * @param roleId 用户角色
	 * @return 返回一个数组：[0]代表组织id集合，[1]代表用户id集合
	 */
	public String[]  getUserPrivilege(String userId,String roleId){
		
		String[] arr = new String[2];
		arr[0] = "'-'";
		arr[1] = "'"+userId+"'";
		
		if(StringUtils.isNullOrEmpty(roleId)){
			//如果不存在角色
			arr[0] = "'-'";
			arr[1] = "'"+userId+"'";
		}
		else if(roleId.equals("100") || roleId.equals("101") || roleId.equals("102")){
			//如果报告编制人员,照片管理员，测试工程师
			arr[0] = "'-'";
			arr[1] = "'"+userId+"'";
		}
		else {
			String org= getUserDepartmentFieldValueByUserId(userId,"STEP");
			if(!org.equals("") && org!=null){
				DataTable dt = getDepartmentsByCondition(org);
				if(dt!=null && dt.getRowCount()>0){
					String orgs="";
					for (DataRow row : dt.getRows()) {
						orgs = StringUtils.linkString(orgs, ",", "'"+row.getString("ID")+"'");
					}
					arr[0] = orgs;
					arr[1] = "'-'";
				}
				
			}
			
		}
		
		return arr;
	}   
	
	public LoginDao getLoginDao() {
		return loginDao;
	}


	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}

}
